home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / dev / gui / FoxGuiSource.lha / FoxLibSource / FoxString.c < prev    next >
C/C++ Source or Header  |  2001-07-08  |  4KB  |  137 lines

  1. /* FoxGUI - The fast, flexible, free Amiga GUI system
  2.     Copyright (C) 2001 Simon Fox (Foxysoft)
  3.  
  4. This library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  5. Foxysoft: www.foxysoft.co.uk      Email:simon@foxysoft.co.uk                */
  6.  
  7.  
  8. /******************************************************************************
  9.  * Shared library code.  Cannot call functions which use exit() such as:
  10.  * printf(), fprintf()
  11.  *
  12.  * Otherwise:
  13.  * The linker returns "__XCEXIT undefined" and the program will fail.
  14.  * This is because you must not exit() a library!
  15.  *
  16.  * Also:
  17.  * proto/exec.h must be included instead of clib/exec_protos.h and
  18.  * __USE_SYSBASE must be defined.
  19.  *
  20.  * Otherwise:
  21.  * The linker returns "Absolute reference to symbol _SysBase" and the
  22.  * library crashes.  Presumably the same is true for the other protos.
  23.  ******************************************************************************/
  24.  
  25. #define __USE_SYSBASE
  26.  
  27. #include <proto/mathieeedoubbas.h>
  28. #include <string.h>
  29. #include <clib/alib_protos.h>
  30. #include <proto/exec.h>
  31.  
  32. #include "/foxinclude/foxgui.h"
  33.  
  34. #ifdef NOT_SHARED_LIBRARY
  35. void spc(FILE *outfp, int spaces)
  36.    {
  37.    char tmp[80];
  38.    strcpy(tmp, "                                                                      ");
  39.    tmp[spaces] = '\0';
  40.    fprintf(outfp, "%s",tmp);
  41.    }
  42. #endif
  43.  
  44. char *RightAlignString(char *str, int lenstr, BOOL commas)
  45.     {
  46.     /* Right align a string of length lenstr (including null terminator).  If commas is TRUE then
  47.         stick in commas every 3 characters as we align it (for numbers). */
  48.     if (str && lenstr)
  49.         {
  50.         str[lenstr - 1] = 0;
  51.         if (strlen(str) < lenstr - 1)
  52.             {
  53.             char *retval;
  54.             // strlen returns an unsigned int so cast it to an int so that if it is 0 length then answer will be -1
  55.             int source = ((int) strlen(str)) - 1, dest = lenstr - 2, placed = 0;
  56.  
  57.             while (source >= 0)
  58.                 {
  59.                 if (placed == 3 && commas)
  60.                     {
  61.                     str[dest--] = ',';
  62.                     placed = 0;
  63.                     }
  64.                 str[dest--] = str[source--];
  65.                 placed++;
  66.                 }
  67.             retval = &str[dest + 1];
  68.             while (dest >= 0)
  69.                 str[dest--] = ' ';
  70.             return retval;
  71.             }
  72.         return str;
  73.         }
  74.     return NULL;
  75.     }
  76.  
  77. static int SetRandomSeed(void)
  78.    {
  79.    int Rubbish = 0, error = FALSE;
  80.     struct timerequest *TimerIO;
  81.     struct MsgPort *TimerMP;
  82.    if (TimerMP = (struct MsgPort *) CreatePort(0, 0))
  83.       {
  84.       if (TimerIO = (struct timerequest *) CreateExtIO(TimerMP, sizeof(struct timerequest)))
  85.          {
  86.          if (!OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *) TimerIO, 0L))
  87.             {
  88.             TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  89.             DoIO((struct IORequest *) TimerIO);
  90.             Rubbish = (int) TimerIO->tr_time.tv_micro;
  91.             CloseDevice((struct IORequest *) TimerIO);
  92.             }
  93.             else
  94.             error = TRUE;
  95.          DeleteExtIO((struct IORequest *) TimerIO);
  96.          }
  97.       else
  98.          error = TRUE;
  99.       DeletePort(TimerMP);
  100.       }
  101.    else
  102.       error = TRUE;
  103. //    if (error)
  104. //        SetLastErr("Random() failed to open timer to set seed - default used.")
  105.    if (Rubbish == 0)
  106.       Rubbish = 1;
  107.    if (Rubbish < 0)
  108.       Rubbish = -Rubbish;
  109.    while (Rubbish > 1024)
  110.       Rubbish /= 2;
  111.    return Rubbish;
  112.    }
  113.  
  114. int RandomSeed = -1;
  115.  
  116. void FOXLIB SetSeed(REGD0 int Seed)
  117.     {
  118.     if (Seed == -1)
  119.         RandomSeed = SetRandomSeed();
  120.     else
  121.         {
  122.         RandomSeed = Seed;
  123.         if (RandomSeed < 0)
  124.             RandomSeed = -RandomSeed;
  125.         if (RandomSeed > 1024)
  126.             RandomSeed = (RandomSeed % 1024) + 1;
  127.         }
  128.     }
  129.  
  130. int FOXLIB Random(REGD0 int limit)
  131. {
  132.    if (RandomSeed == -1)
  133.          RandomSeed = SetRandomSeed();   /* Can be any number between 1 and 1024 */
  134.     RandomSeed = ((29 * RandomSeed) + 7) % 1024;
  135.     return (int) (limit * (RandomSeed / 1024.0)) + 1;
  136. }
  137.